<template>
	<view class="container">
		<view class="list-cell b-b m-t"   >
			<text class="cell-tit">头像</text>
			<view class="flex ali-c">
				<!-- #ifdef MP-WEIXIN -->
				<button class="touximg"  open-type="chooseAvatar" @chooseavatar="onChooseAvatar" >
					<image class="imagetxs" :src="userInfo.avatar" mode="aspectFit"></image>
				</button>
				<!-- #endif -->
				<!-- #ifndef MP-WEIXIN -->
				<image class="imagetxs" v-if="userInfo.avatar" :src="photo_cdn1+userInfo.avatar" mode="aspectFit"></image>
				<image class="imagetxs" v-else-if="userInfo.gender==1" :src="photo_cdn1 + 'zpupload/static/tablet/boy_alive.png'" mode=""></image>
				<image class="imagetxs" v-else :src="photo_cdn1 + 'zpupload/static/tablet/girl_alive.png'" mode=""></image>
				<!-- #endif -->
				
				<!-- <image class="jiantou ml-10" src="/s1tatic/user/jiantoua.png" mode="aspectFit"></image> -->
			</view>
		</view>
		<view class="list-cell b-b"   >
			<text class="cell-tit">昵称</text>
			<view class="flex ali-c">
				
				<!-- #ifdef MP-WEIXIN -->
				<input type="nickname"   @change="getNickname"  class="valtext valtextnikename" v-model="userInfo.nickname" placeholder="请输入昵称"/>
				<!-- #endif -->
				
				<!-- #ifndef MP-WEIXIN -->
				<text class="valtext">{{userInfo.nickname}}</text>
				<!-- #endif -->
				
				
				<!-- <text class="valtext">{{userInfo.nickname}}</text> -->
				<!-- <image class="jiantou ml-10" src="/s1tatic/user/jiantoua.png" mode="aspectFit"></image> -->
			</view>
		</view>
		<view class="list-cell b-b"  >
			<text class="cell-tit">手机号</text>
			<view class="flex ali-c">
				<text class="valtext">{{userInfo.mobile?userInfo.mobile:""}}</text>
				<!-- <image class="jiantou ml-10" src="/s1tatic/user/jiantoua.png" mode="aspectFit"></image> -->
			</view>
		</view>
		<view class="list-cell b-b" >
			<text class="cell-tit">用户id</text>
			<view class="flex ali-c">
				<text class="valtext">{{userInfo.id}}</text>
				<!-- <image class="jiantou ml-10" src="/s1tatic/user/jiantoua.png" mode="aspectFit"></image> -->
			</view>
		</view>


		
		<!-- <navigator url="/pages/usermanage/Paypassword/Paypassword" class="list-cell m-t " hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">支付密码</text>
			<view class="flex ali-c">
				<text class="valtext" style="color: #888;">设置/修改支付密码</text>
				<image class="jiantou ml-10" src="/s1tatic/user/jiantoua.png" mode="aspectFit"></image>
			</view>
		</navigator> -->
		
		<!-- #ifdef APP-PLUS -->
		<view class="list-cell m-t " @tap="jiancegx" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">检测更新</text>
			<view class="flex ali-c">
				<image class="jiantou ml-10" :src="photo_cdn1 + 'zpupload/static/user/jiantoua.png'" mode="aspectFit"></image>
			</view>
		</view>
		
		
		<view class="list-cell m-t " @tap="logoutuser" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">注销账号</text>
			<view class="flex ali-c">
				<image class="jiantou ml-10" :src="photo_cdn1 + 'zpupload/static/user/jiantoua.png'" mode="aspectFit"></image>
			</view>
		</view>
		
		<!-- 关闭定向推送 -->
		<view class="list-cell m-t " @tap="gbtsfun" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">{{close_dxtz==1?"关闭定向推送":"开启定向推送"}}</text>
			<view class="flex ali-c">
				<text class="valtext" style="color: #888;">当前状态为:{{close_dxtz==1?'开启':"关闭"}}</text>
				<image class="jiantou ml-10" :src="photo_cdn1 + 'zpupload/static/user/jiantoua.png'" mode="aspectFit"></image>
			</view>
		</view>
		
		
		
		<view class="list-cell m-t " @tap="bindweixn" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">绑定微信</text>
			<view class="flex ali-c">
				<text class="valtext" style="color: #888;">用于提现到微信</text>
				<image class="jiantou ml-10" :src="photo_cdn1 + 'zpupload/static/user/jiantoua.png'" mode="aspectFit"></image>
			</view>
		</view>
		
		<!-- #endif -->
		
		
		<view class="list-cell m-t " @tap="outlogin" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">退出登录</text>
			<view class="flex ali-c">
				<image class="jiantou ml-10" :src="photo_cdn1 + 'zpupload/user/jiantoua.png'" mode="aspectFit"></image>
			</view>
		</view>
		
		
		<view v-if="issho_save" @tap="saveuuinfo" class="savenamebtns">保存</view>
		
		
		<appupdate ref="appupdate" @tisnovis="tisnovis"></appupdate>
	</view>
</template>

<script>
	import {getUserinfo,getAvatar} from "@/utils/api/api.js";
	// import {uploadFile,photo_cdn2} from "@/utils/config.js";
	
	import config from "@/utils/config.js";
	let {uploadFile,photo_cdn2,photo_cdn1} = config.baseUrl;
	
	
	import {authorWechat} from "@/utils/api/api.js";
	import appupdate from "@/components/appupdate/appupdate.vue";
	
	
	
	export default {
		data() {
			return {
				photo_cdn1,
				photo_cdn2,
				userInfo:{},
				close_dxtz:"1",//关闭定向通知的开关  1开启中  2关闭中
				
				issho_save:false,
				nikenamestr:"",//设置的昵称
				avatarUrl_str:"",//设置头像
				
				isloginz:true,//是否再登录中
				returninfo:{},
				userInfo_sub:{},
				access_token:"",
			};
		},
		components:{
			// appupdate,
		},
		onLoad(options) {
			this.getUserInfofun();
			
			//首页过来的 自动检测更新
			if(options.toupdate==1){
				setTimeout(()=>{
					this.jiancegx();
				},700)
			}
		},
		onShow() {
			let close_dxtz =  uni.getStorageSync("close_dxtz");
			this.close_dxtz = close_dxtz?close_dxtz:"1";
		},
		methods:{
			tisnovis(){
				this.$api.msg("已是最新版本!");
			},
			jiancegx(){
				// #ifdef APP-PLUS
				//检测更新
				setTimeout(()=>{
					this.$refs.appupdate.update();//请求更新
				},1000)
				// #endif
			},
			outlogin(){
				uni.setStorageSync("token","")
				uni.setStorageSync("u_userInfo","")
				uni.clearStorage();//全部清楚
				uni.reLaunch({
					url:"/pagesim/center/login",
				})
			},
			//注销账号 
			logoutuser(){
				this.$showModal({
					title:'提示',
					concent:'确定要注销账号吗?',
					confirmVal:'确定',
					cancelVal:'取消',
				}).then(res=>{
					this.getUserInfofun();
					uni.setStorageSync("token","")
					uni.setStorageSync("u_userInfo","")
					uni.reLaunch({
						url:"/pages/login/login",
					})
				})
			},
			//关闭推送
			gbtsfun(){
				this.$showModal({
					title:'提示',
					concent:`你确定要${this.close_dxtz==1?"关闭":"开启"}推送消息吗?`,
					confirmVal:'确定',
					cancelVal:'取消',
				}).then(res=>{
					this.getUserInfofun();
					this.$api.msg("操作成功");
					
					if(this.close_dxtz==1){
						this.close_dxtz = 2;
					}else{
						this.close_dxtz = 1;
					}
					uni.setStorageSync("close_dxtz",this.close_dxtz)
				})
			},
			//获取用户信息
			getUserInfofun(){
				// let userInfo = uni.getStorageSync("u_userInfo");
				// if(userInfo){
				// 	this.userInfo = userInfo;
				// }
				
				getUserinfo({
					gb:1,// 给审核看的
					gbts:1,
				}).then((res)=>{
					if(res.code==1){
						uni.getStorageSync("u_userInfo",res.data.userinfo);
						this.userInfo = res.data.userinfo;
					}
				})
			},
			//设置头像
			onChooseAvatar(e) {
				const {
					avatarUrl
				} = e.detail;
				
				uni.uploadFile({
				    url: uploadFile, 
				    filePath: avatarUrl,
				    name: 'file',
				    formData: {
				       "Content-Type": "multipart/form-data",
				       water: 0
				    },
				    success: (res) => {
						let data = JSON.parse(res.data)
						
						let avatarUrl_str = this.photo_cdn2 + data.data.url; //上传后完整的url
						let userInfo = this.userInfo;
						userInfo.avatar = avatarUrl_str;
						this.userInfo = userInfo;
						this.issho_save = true;
						this.avatarUrl_str = avatarUrl_str;
						
				    },
					fail: (err) => {
						uni.showToast({
							title: '上传失败',
							icon: 'none'
						})
					}
				});
		
			},
			//type=“nickname“ 获取昵称
			getNickname(e){
				if(e.detail.value){
					let userInfo = this.userInfo;
					userInfo.nickname = e.detail.value;
					this.userInfo = userInfo;
					this.issho_save = true;
					this.nikenamestr = e.detail.value;
					
				}
			},
			//保存微信小程序设置的信息
			saveuuinfo(){
				let {avatarUrl_str,nikenamestr} = this;
				if(!avatarUrl_str && !nikenamestr){
					//两个都没设置 那就不能提交
					this.issho_save = false;
					return false;
				}
		
				if(!avatarUrl_str){
					avatarUrl_str = this.userInfo.face;//只修改一个 另外一个填补原来的
				}
				if(!nikenamestr){
					nikenamestr = this.userInfo.nickname;//只修改一个 另外一个填补原来的
				}
				let parmas = {
					nickname:nikenamestr,
					avatar:avatarUrl_str,
				}

				getAvatar(parmas).then((res)=>{
					if(res.code==1){
						uni.showToast({
							title: '保存成功',
							icon:"none"
						})
		
						this.issho_save = false;
						this.nikenamestr = "";
						this.avatarUrl_str = "";
						setTimeout(() => {
							uni.navigateBack();
						}, 1500);
					}else{
						wx.showToast({
							title: res.msg,
							icon:"none"
						})
					}
				})
			},
			//绑定微信
			bindweixn(){
				if(this.isloginz){
					this.isloginz = false;
					uni.getProvider({
						service: 'oauth',
						success: (res)=> {
							console.info("555");
							//支持微信、qq和微博等
							if (~res.provider.indexOf('weixin')) {
							
								uni.login({
									provider: 'weixin',
									success: (loginRes)=> {
										
										this.access_token = loginRes.authResult.access_token;
										uni.getUserInfo({// 获取用户信息
											provider: 'weixin',
											success: (infoRes)=> {
												console.info("888");
												uni.hideLoading();
												this.returninfo = JSON.stringify(infoRes);
												let obj = infoRes.userInfo
												this.userInfo_sub = obj;
												this.apple_login1();//吧获取到的信息传到后台
											},
											fail:()=>{
												console.info("999");
												this.isloginz = true;
												uni.hideLoading();
											}
										});
									},
									fail:()=>{
										console.info("9991");
										this.isloginz = true;
										uni.hideLoading();
									},
									complete:()=> {
										this.isloginz = true;
										
										uni.hideLoading();
										
									}
								});
								
								//防止取消的情况 这个状态没办法释放
								setTimeout(()=>{
									this.isloginz = true;
								},1500)
							}else{
								// this.isloginz = true;
								// this.userInfo = this.bebuginfo.userinfo;
								// that.apple_login1();//吧获取到的信息传到后台
								// console.info("9992");
								// uni.hideLoading();
							}
						},
						fail:()=> {
							console.info("9996");
							this.isloginz = true;
							uni.hideLoading();
							
						}
					});
				}
				
			},
			//将授权得到的数据传给后端
			apple_login1(){
				let info=uni.getSystemInfoSync();
				let obj = this.userInfo_sub;
				let objs = {
					access_token:this.access_token?this.access_token:'51_bQunAxbzB_Y74oJfryDBcYBU2DqXGbYEB7HHSH4nhGKRpwbQPjNxBEm3ZSk3AF63XpaWPlvqQTjNkOsoMy8lGqppa37vahZWVD2NAquWSeE',//微信的token
					userinfo:obj,//用户信息
					// version:this.version,//版本号
					// brand:info.brand ,//机型 华为 小米 等等
					// platform:this.platform,//安卓还是ios
				}

				// objs.userinfo.nickname = objs.userinfo.nickName;
				// objs.userinfo.openid = objs.userinfo.openId;
				// objs.userinfo.avatarurl = objs.userinfo.avatarUrl;
				// objs.userinfo.unionid = objs.userinfo.unionId;
				
				console.info("objs",objs);
				// console.info("userInfo_sub",this.userInfo_sub);
				
				// return;
				authorWechat({
					...objs,
					// userinfo:this.userInfo_sub,
				}).then((res)=>{
					console.info("res111111",res);
					// console.info("objs",objs);
					if(res.code==1){
						this.$api.msg("登录成功");
					}else{
						this.$api.msg(res.msg);
					}
				})
			},
		}
	}
</script>

<style lang='scss'>
	
	page{
		background: #f8f8f8;
	}
	.list-cell{
		display:flex;
		align-items:center;
		padding: 20upx 30upx;
		line-height:60upx;
		position:relative;
		background: #fff;
		justify-content: center;
		
		.jiantou{
			width: 13upx;
			height: 24upx;
		}
		&.log-out-btn{
			margin-top: 40upx;
			.cell-tit{
				color: #fa436a;
				text-align: center;
				margin-right: 0;
			}
		}
		&.cell-hover{
			background:#fafafa;
		}
		&.b-b:after{
			position: absolute;
			z-index: 3;
			right: 0;
			bottom: 0;
			content: '';
			height: 1px;
			background-color: #E4E7ED;
		}
		&.b-b:after{
			left: 30upx;
		}
		&.m-t{
			margin-top: 16upx; 
		}
		.cell-more{
			align-self: baseline;
			font-size:32upx;
			color:#909399;
			margin-left:10upx;
		}
		.cell-tit{
			flex: 1;
			font-size: 30upx;
			color: #303133;
			margin-right:10upx;
		}
		.cell-tip{
			font-size:28upx;
			color: #909399;
		}
		switch{
			transform: translateX(16upx) scale(.84);
		}
		
		.imagetxs{
			width: 100upx;
			height: 100upx;
			border-radius: 50%;
		}
		.valtext{
			color: #000;
			font-size: 28upx;
		}
		.valtextnikename{
			text-align: right;
		}
	}
	.touximg{
		width:100rpx;
		height:100rpx;
		border-radius:50%;
		border: 0;
		padding: 0;
		margin: 0;
		background-color: transparent;
	}
	.savenamebtns{
	    margin: 30rpx auto;
	    width: 690rpx;
	    height: 88rpx;
	    line-height: 88rpx;
	    background-color: #227afc;
	    color: #fff;
	    border-radius: 40rpx;
	    text-align: center;
	    font-size: 30rpx;
	}
</style>
